import { appLogin, getUserInfo, noticeReal } from "@/config/api";
import { userLogin } from "@/config/api-java";
import { baseUrl } from "@/config/config";
const state = {
  token: uni.getStorageSync(baseUrl),
  nickName: null,
  avatar: null,
  mobile: null,
  id: null,
  openId: null,
  point: null,
  addressId: null,
  address: null,
  noticeCount: 0,
  ticket: null,
  identify: 0,
  realName: null,
  realIdCard: null,
  inviteNum: null,
  inviteCode: null,
  buy: null,
  mpOpenId: uni.getStorageSync("mpOpenId"),
  userTicketCount: 0,
  nftOwnerCount: 0,
  balance: 0,
  followNum: 0,
  fanNum: 0,
  walletId: null,
  payment: null,
  choiceNftCode: null,
  safePass: null,
  readNotice: uni.getStorageSync("readNotice"),
  agreeMarket: uni.getStorageSync("agreeMarket")
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NICK_NAME: (state, nickName) => {
    state.nickName = nickName;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_MOBILE: (state, mobile) => {
    state.mobile = mobile;
  },
  SET_ID: (state, id) => {
    state.id = id;
  },
  SET_OPENT_ID: (state, openId) => {
    state.openId = openId;
  },
  SET_POINT: (state, point) => {
    state.point = point;
  },
  SET_ADDRESS_ID: (state, addressId) => {
    state.addressId = addressId;
  },
  SET_ADDRESS: (state, address) => {
    state.address = address;
  },
  SET_NOTICE_COUNT: (state, noticeCount) => {
    state.noticeCount = noticeCount;
  },
  SET_TICKET: (state, ticket) => {
    state.ticket = ticket;
  },
  SET_IDENTIFY: (state, identify) => {
    state.identify = identify;
  },
  SET_REAL_NAME: (state, realName) => {
    state.realName = realName;
  },
  SET_REAL_ID_CARD: (state, realIdCard) => {
    state.realIdCard = realIdCard;
  },
  SET_MP_OPEN_Id: (state, mpOpenId) => {
    state.mpOpenId = mpOpenId;
  },
  SET_INVITE_NUM: (state, inviteNum) => {
    state.inviteNum = inviteNum;
  },
  SET_BUY: (state, buy) => {
    state.buy = buy;
  },
  SET_INVITE_CODE: (state, inviteCode) => {
    state.inviteCode = inviteCode;
  },
  SET_USER_TICKET_COUNT: (state, userTicketCount) => {
    state.userTicketCount = userTicketCount;
  },
  SET_NFT_OWNER_COUNT: (state, nftOwnerCount) => {
    state.nftOwnerCount = nftOwnerCount;
  },
  SET_BALANCE: (state, balance) => {
    state.balance = balance;
  },
  SET_FOLLOW_NUM: (state, followNum) => {
    state.followNum = followNum;
  },
  SET_FAN_NUM: (state, fanNum) => {
    state.fanNum = fanNum;
  },
  SET_WALLET_ID: (state, walletId) => {
    state.walletId = walletId
  },
  SET_PAYMENT: (state, payment) => {
    state.payment = payment
  },
  SET_CHOICE_NFT_CODE: (state, choiceNftCode) => {
    state.choiceNftCode = choiceNftCode
  },
  SET_SAFE_PASS: (state, safePass) => {
    state.safePass = safePass
  },
  SET_READ_NOTICE: (state, readNotice) => {
    state.readNotice = readNotice
  },
  SET_AGREE_MARKET: (state, agreeMarket) => {
    state.agreeMarket = agreeMarket
  }
};

const actions = {
  login({ commit, dispatch }) {
    uni.getUserProfile({
      desc: "获取信息记录用户登录状态",
      success: (res) => {
        return new Promise((resolve, reject) => {
          uni.login({
            provider: "weixin",
            success: (loginRes) => {
              appLogin({ code: loginRes.code, ...res.userInfo })
                .then((res) => {
                  commit("SET_NICK_NAME", res.userInfo.nickName);
                  commit("SET_AVATAR", res.userInfo.avatar);
                  commit("SET_MOBILE", res.userInfo.mobile);
                  commit("SET_ID", res.userInfo.id);
                  commit("SET_OPENT_ID", res.userInfo.openId);
                  commit("SET_POINT", res.userInfo.point);
                  commit("SET_ADDRESS_ID", res.userInfo.addressId);
                  commit("SET_TICKET", res.ticket);
                  commit("SET_IDENTIFY", res.userInfo.identify);
                  dispatch("setToken", res.token);
                  if (!res.userInfo.mobile) {
                    uni.$u.route("pages/binding/index");
                  }
                  resolve(res);
                })
                .catch((error) => {
                  reject(error);
                });
              //than.navigateTo("pages/binding/index")
            },
          });
        });
      },
    });
  },
  webLogin({ commit, dispatch }, data) {
    return new Promise((resolve, reject) => {
      userLogin(data)
        .then((res) => {
          /*commit("SET_NICK_NAME", res.user.nickName);
          commit("SET_AVATAR", res.user.avatar);
          commit("SET_MOBILE", res.user.mobile);
          commit("SET_ID", res.user.id);
          commit("SET_OPENT_ID", res.user.openId);
          commit("SET_POINT", res.user.point);
          commit("SET_ADDRESS_ID", res.user.addressId);
          commit("SET_IDENTIFY", res.user.identify);
          commit("SET_REAL_NAME", res.user.realName);
          commit("SET_REAL_ID_CARD", res.user.realIdCard);
          commit("SET_TICKET", res.ticket);
          commit("SET_INVITE_CODE", res.user.inviteCode);
          commit("SET_INVITE_NUM", res.user.inviteNum);*/
          dispatch("setToken", res.token).then((token) => {
            dispatch("info").then((userInfo) => {
              resolve(userInfo);
            });
          });
        })
        .catch((error) => {
          reject(error);
        });
      //than.navigateTo("pages/binding/index")
    });
  },
  info({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo({ custom: { catch: true, auth: true } })
        .then((userInfo) => {
          commit("SET_NICK_NAME", userInfo.nickName);
          commit("SET_AVATAR", userInfo.avatar);
          commit("SET_MOBILE", userInfo.mobile);
          commit("SET_ID", userInfo.id);
          commit("SET_OPENT_ID", userInfo.openId);
          commit("SET_POINT", userInfo.user_wallet ? userInfo.user_wallet.point : 0);
          if (userInfo.user_wallet) {
            commit("SET_BALANCE", userInfo.user_wallet.balance);
            commit("SET_WALLET_ID", userInfo.user_wallet.id)
          }
          commit("SET_ADDRESS_ID", userInfo.addressId);
          commit("SET_IDENTIFY", userInfo.identify);
          commit("SET_REAL_NAME", userInfo.realName);
          commit("SET_REAL_ID_CARD", userInfo.realIdCard);
          commit("SET_NOTICE_COUNT", userInfo.notice_count);
          commit("SET_INVITE_NUM", userInfo.inviteNum);
          commit("SET_BUY", userInfo.buy);
          commit("SET_INVITE_CODE", userInfo.inviteCode);
          commit("SET_USER_TICKET_COUNT", userInfo.user_ticket_count);
          commit("SET_NFT_OWNER_COUNT", userInfo.nft_owner_count);
          commit("SET_FOLLOW_NUM", userInfo.followNum? userInfo.followNum:0);
          commit("SET_FAN_NUM", userInfo.fanNum? userInfo.fanNum:0);
          commit("SET_SAFE_PASS", userInfo.safePass)
          resolve(userInfo);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  setToken({ commit }, token) {
    return new Promise((resolve, reject) => {
      commit("SET_TOKEN", token);
      uni.setStorageSync(baseUrl, token);
      resolve();
    });
  },
  setPayment({commit}, payment) {
    return new Promise((resolve, reject) => {
      commit("SET_PAYMENT", payment);
      resolve();
    });
  },
  setBalance({commit}, balance) {
    return new Promise((resolve, reject) => {
      commit("SET_BALANCE", balance);
      resolve();
    });
  },

  setChoiceNftCode({commit}, choiceNftCode) {
    return new Promise((resolve, reject) => {
      commit("SET_CHOICE_NFT_CODE", choiceNftCode);
      resolve();
    });
  },
  setUserNotice({commit}) {
    return new Promise((resolve, reject) => {
      noticeReal().then((res) => {
        commit("SET_NOTICE_COUNT", 0);
        resolve();
      })
    })
  },
  setAgreeMarket({commit}) {
    uni.setStorageSync("agreeMarket", true);
    commit("SET_AGREE_MARKET", true);
  },
  setTicket({ commit }, ticket) {
    return new Promise((resolve, reject) => {
      commit("SET_TICKET", ticket);
      resolve();
    });
  },
  setPhone({ commit }, mobile) {
    return new Promise((resolve, reject) => {
      commit("SET_MOBILE", mobile);
      resolve();
    });
  },
  setReadNotice({ commit }, readNotice) {
    uni.setStorageSync("readNotice", readNotice);
    commit("SET_READ_NOTICE", readNotice)
  },
  setSafePass({ commit }, safePass) {
    commit("SET_SAFE_PASS", true)
  },
  setAvatar({ commit }, avatar) {
    commit("SET_AVATAR", avatar)
  },
  setNickName({ commit }, nickname) {
    commit("SET_NICK_NAME", nickname)
  },
  selectAddress({ commit }, address) {
    return new Promise((resolve, reject) => {
      commit("SET_ADDRESS", address);
      resolve();
    });
  },
  setDefaultAddress({ commit }, addressId) {
    return new Promise((resolve, reject) => {
      commit("SET_ADDRESS_ID", addressId);
      resolve();
    });
  },
  setIdentify({ commit }, data) {
    return new Promise((resolve, reject) => {
      commit("SET_IDENTIFY", data.identify);
      commit("SET_REAL_NAME", data.realName);
      commit("SET_REAL_ID_CARD", data.realIdCard);
      resolve();
    });
  },
  loginout({ commit }) {
    return new Promise((resolve, reject) => {
      commit("SET_NICK_NAME", null);
      commit("SET_AVATAR", null);
      commit("SET_MOBILE", null);
      commit("SET_ID", null);
      commit("SET_OPENT_ID", null);
      commit("SET_POINT", 0);
      commit("SET_ADDRESS_ID", null);
      commit("SET_IDENTIFY", null);
      commit("SET_REAL_NAME", null);
      commit("SET_REAL_ID_CARD", null);
      commit("SET_NOTICE_COUNT", 0);
      uni.removeStorageSync(baseUrl);
      commit("SET_TOKEN", null);
      uni.$u.route("pages/login/index");
      resolve(true);
    });
  },
  setMpOpenId({ commit }, openId) {
    return new Promise((resolve, reject) => {
      commit("SET_MP_OPEN_Id", openId);
      uni.setStorageSync("mpOpenId", openId);
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
